package cc.telecomdigital.tdstock.trading.widget;

import cc.telecomdigital.tdstock.R;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class AppDialogImgOKCancel extends Dialog implements
	Button.OnClickListener, OnCancelListener
{
	public interface IAppDialogImgOKCancel
	{
		/** What to do when an OK is pressed **/
		void OnOK_Click(DialogInterface iDialog);

		/** What to do when Canel is pressed **/
		void OnCancel_Click(DialogInterface iDialog);

		/** What to do when user pressed back button **/
		void OnDialogCancel(DialogInterface iDialog);
	}

	private IAppDialogImgOKCancel	interfaceHandle;
	
	/** Place in the owner and image to be shown in the middle of the Dialog box **/
	public AppDialogImgOKCancel(Context iContext, int iImageId)
	{
		super(iContext, R.style.TradeDialog_Style);
		setContentView(R.layout.app_dialog_imgokcancel);

		ImageView imgViewObj;

		imgViewObj = (ImageView) (findViewById(R.id.app_dialog_imgokcancel_Image));
		imgViewObj.setImageResource(iImageId);		
	}
	
	/** Set Dialog Box content **/
	public void SetTextContent(String iContent)
	{
		TextView textViewObj;

		textViewObj = (TextView) findViewById(R.id.app_dialog_imgokcancel_Content);
		iContent = iContent.replace("\\n", "\n");
		textViewObj.setText(iContent);			
	}

	/** Set Dialog Box content from a resource id **/	
	public void SetTextContent(int iStringId)
	{
		String tmpString;

		tmpString = getContext().getResources().getString(iStringId);
		SetTextContent(tmpString);
	}

	/** Shows the dialog by putting in the return interface
	    The Dialog will be dismissed once either one of the action is done. **/
	public void show(IAppDialogImgOKCancel iInterfaceHandle,
			boolean iCancelable)
	{
		
		Button buttonObj;

		buttonObj = (Button) findViewById(R.id.app_dialog_imgokcancel_OKButton);
		buttonObj.setOnClickListener(this);

		buttonObj = (Button) findViewById(R.id.app_dialog_imgokcancel_CancelButton);
		buttonObj.setOnClickListener(this);

		interfaceHandle = iInterfaceHandle;

		this.setOnCancelListener(this);
		this.setCancelable(iCancelable);
		this.show();
	}

	@Override
	public void onClick(View iViewObj)
	{
		switch (iViewObj.getId())
		{
			case R.id.app_dialog_imgokcancel_OKButton:
				this.dismiss();
				if (interfaceHandle != null)
					interfaceHandle.OnOK_Click(this);
				break;
			case R.id.app_dialog_imgokcancel_CancelButton:
				this.dismiss();
				if (interfaceHandle != null)
					interfaceHandle.OnCancel_Click(this);
				break;
		}
	}

	@Override
	public void onCancel(DialogInterface iDialog)
	{
		// TODO Auto-generated method stub
		iDialog.dismiss();
		if (interfaceHandle != null)
			interfaceHandle.OnDialogCancel(iDialog);
	}
}
